package com.kucai.yifaqapp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.fragment.DaggerDepositeFragmentComponent;
import com.kucai.yifaqapp.dagger.module.fragment.DepositeFragmentModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.Pay_CunKuanList;
import com.kucai.yifaqapp.model.network.entity.res.z_moneyDetail;
import com.kucai.yifaqapp.presenter.fragment.DepositeFragmentPresenter;
import com.kucai.yifaqapp.ui.fragment.gameFragment.MoneydetailDialog;
import com.kucai.yifaqapp.ui.view.DepositeFragmentView;
import com.kucai.yifaqapp.util.OnItemClickListener;

import butterknife.BindView;

/**
 * 存款记录
 */
public class DepositeFragment extends BaseFragment<DepositeFragmentPresenter> implements DepositeFragmentView {
    @BindView(R.id.deposite_rl)
    RecyclerView depositeRl;
    private MyAdapter activtyAdapter;
    private Pay_CunKuanList mDatas;

    @Override
    protected void ChangeSkin() {

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_deposite;
    }

    @Override
    protected void initDagger() {
        DaggerDepositeFragmentComponent.builder()
                .depositeFragmentModule(new DepositeFragmentModule(this))
                .build().inject(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        InitData();
        InitView();
    }

    private void InitView() {
        //活动列表view
        //添加布局管理器
        depositeRl.setLayoutManager(new LinearLayoutManager(getContext()));
//        homeRl.setLayoutManager(linearLayoutManager);
        //创建适配器
        activtyAdapter = new MyAdapter();
    }

    private void InitData() {
        if (Info.user != null) {
            mPresenter.getCunKuanList(Info.user.token);
        }
    }

    @Override
    public void onsuccess(Pay_CunKuanList list) {
        mDatas = list;
        if (mDatas != null) {
            if (mDatas.getData() != null) {
                depositeRl.setAdapter(activtyAdapter);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.deposite_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            try {
                holder.time_tv.setText(mDatas.getData().get(position).getStatDate());
                holder.rechmoney.setText(mDatas.getData().get(position).getRechMoney());
//                status : 状态 1未受理 2充值成功 3充值失败 4在线充值中
                switch (mDatas.getData().get(position).getStatus()) {
                    case 1:
                        holder.states.setText("未受理");
                        break;
                    case 2:
                        holder.states.setText("充值成功");
                        break;
                    case 3:
                        holder.states.setText("充值失败");
                        break;
                    case 4:
                        holder.states.setText("在线充值中");
                        break;
                }
                holder.itemclick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        z_moneyDetail.bean = mDatas.getData().get(position);
                        startActivity(new Intent(getActivity(), MoneydetailDialog.class));
                    }
                });
            } catch (Exception e) {

            }
        }

        @Override
        public int getItemCount() {
            if (mDatas.getData() == null) {
                return 0;
            } else {
                return mDatas.getData().size();
            }
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView time_tv;
            TextView rechmoney;
            TextView states;
            LinearLayout itemclick;

            MViewHolder(View itemView) {
                super(itemView);
                itemclick = itemView.findViewById(R.id.itemclick);
                time_tv = (TextView) itemView.findViewById(R.id.time_tv);
                rechmoney = itemView.findViewById(R.id.rechmoney);
                states = itemView.findViewById(R.id.states);

            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //刷新操作
            if (Info.user != null) {
                mPresenter.getCunKuanList(Info.user.token);
            }
        }
    }
}
